<?php
/**
 * V7 check 1
 *
 */
class data_article extends data {
	public function __construct() {
		parent::__construct();
	}
	// article url 处理：url id转换，是否存在，写入，++计数 --------------------------------------
	/**
	 * 返回文章url的id，如果不存在，则写入并返回insert id
	 *
	 * @param string $url
	 * @return int
	 */
	protected function url_to_id_or_insert ($url) {
		$url_id = $this->url_to_id($url);
		if ($url_id) {
			return $url_id;
		} else {
			$url_id = $this->add_url($url);
			return $url_id;
		}
	}
	/**
	 * 返回文章url的id，不存在返回false
	 * 别名：is_url_exist
	 *
	 * @param string $url
	 * @return int
	 */
	protected function url_to_id ($url) {
		if ($url) {
			$url_md5 			= md5($url);
			$execute = $this->db->execute('select 
												id 
											from 
												article_url 
											where 
												url_md5				= "'.$url_md5.'" 
											limit 
												1
										');
			if ($execute[0]['id']) {
				return $execute[0]['id'];
			} else {
				false;
			}
		} else return false;
	}
	/**
	 * url_to_id 的别名
	 *
	 * @param string $url
	 * @return int
	 */
	protected function is_url_exist ($url) {
		return $this->url_to_id($url);
	}
	/**
	 * 添加文章url，返回insert id
	 *
	 * @param string $url
	 * @return int
	 */
	protected function add_url ($url) {
		$url = addslashes(stripslashes(trim($url)));
		if ($url) {
			$url_md5 			= md5($url);
			$insert_id = $this->db->execute('insert into 
													article_url
												set
													url					= "'.$url.'",
													url_md5				= "'.$url_md5.'",
													saved_times			= 0
										');
			if ($insert_id) {
				return $insert_id;
			} else return false;
		} else return false;
	}
	/**
	 * 增加url的统计数
	 *
	 * @param int $url_id
	 * @return bool
	 */
	public function event_url_increase ($url_id) {
		$url_id = intval($url_id);
		
		if ($url_id) {
			$update = $this->db->execute('update 
												article_url 
											set 
												saved_times = saved_times + 1 
											where 
												id = "'.$url_id.'"
										');
			return $update;
		} else return false;
	}
	/**
	 * 获取一条存档文章的正文
	 *
	 * @param int $id
	 * @return string
	 */
	public function get_save_article_content ($id) {
		$id = intval($id);
		
		if ($id) {
			$execute = $this->db->execute('select content from save_article_content where id="'.$id.'"');
			return $execute[0]['content'];
		} else return false;
	}
	public function set_save_article_content ($id, $content) {
		$id			= intval($id);
		
		if ($id and $content) {
			$content = addslashes($content);
			$exist = $this->db->execute('select id from save_article_content where id="'.$id.'"');
			if ($exist) {
				return $this->db->execute('
					update
						save_article_content
					set
						content		= "'.$content.'"
					where
						id			= "'.$id.'"
				');
			} else {
				$sql = '
					insert into
						save_article_content
					values
						("'.$id.'", "'.$content.'")
				';
				return $this->db->execute($sql);
			}
		} else return false;
	}
	// 这个没用到
	protected function del_save_article_content ($id) {
		$id		= intval($id);
		
		if ($id) {
			return true;
		} else return false;
	}
	/**
	 * 当站内存档的时候，把content复制过来，把clip也复制过来
	 *
	 * @param int $new_id
	 * @param int $content_id
	 * @return bool
	 */
	protected function copy_content_to_save ($new_id, $content_id) {
		$new_id		= intval($new_id);
		$content_id		= intval($content_id);
		
		if ($new_id and $content_id) {
			$this->db->execute('
				insert into
					content_clip
				(id, clip)
					select
						"'.$new_id.'", clip
					from	
						content_clip
					where
						id = "'.$content_id.'"
			');
			return $this->db->execute('
				insert into
					save_article_content
				(id, content)
					select
						"'.$new_id.'",
						content
					from
						save_article_content
					where
						id = "'.$content_id.'"
			');
		} else return false;
	}
	public function get_content_clip ($id) {
		$id = intval($id);
		if ($id) {
			$execute = $this->db->execute('
				select clip from content_clip where id = "'.$id.'"
			');
			return $execute[0]['clip'];
		} else return false;
	}
	public function set_content_clip ($id, $content) {
		$id			= intval($id);
		
		if ($id) {		// 即使content为空也要写入
			$content = addslashes($content);
			$exist = $this->db->execute('select id from content_clip where id="'.$id.'"');
			if ($exist) {
				return $this->db->execute('
					update
						content_clip
					set
						clip		= "'.$content.'"
					where
						id		= "'.$id.'"
				');
			} else {
				$sql = '
					insert into
						content_clip
					values
						("'.$id.'", "'.$content.'")
				';
				return $this->db->execute($sql);
			}
		} else return false;
	}
	/**
	 * 为了给人人分享抓取title
	 * 
	 * @param Int $id
	 * @return array
	 */
	public function get_save_article_title_and_summary ($id) {
		$id = intval($id);
		if ($id) {
			$execute = $this->db->execute('
				select title, summary from save_article where id = "'.$id.'"
			');
			if ($execute[0]['title']) {
				return $execute[0];
			} else return false;
		} else return false;
	}
}
?>